<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形控件 -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--#default === slot-scope什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染) -->
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon
                  v-if="scope.data.children.length === 0"
                  icon-class="ren"
                />
                <svg-icon v-else :icon-class="scope.node.expanded ? 'jianhao' : 'jiahao'" />
                <!-- <svg-icon icon-class="wechat" /> -->
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog :title="isEdit?'编辑部门':'新增部门'" :visible.sync="showDialog" :close-on-click-modal="false">
      <depDialog v-if="showDialog" :pid="pid" :is-edit="isEdit" :origin-list="originList" @success="recSuccess" @close="recClose" />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { array2Tree } from '@/utils/index'
import depDialog from '@/views/departments/depDialog.vue'
export default {
  // ------------------------------------------------------------------------------------------------------------------注册组件
  components: { depDialog },
  // ------------------------------------------------------------------------------------------------------------------数据
  data() {
    return {
      isEdit: false,
      // ---------------------------------------------------------------对话框开关
      showDialog: false,
      // ---------------------------------------------------------------树形数据
      list: [],
      // ---------------------------------------------------------------传递给子组件id
      pid: '',
      // ---------------------------------------------------------------给子组件传递的值，用于表单自定义校验
      originList: []
    }
  },
  // ------------------------------------------------------------------------------------------------------------------created钩子
  created() {
    // ---------------------------------------------------------------刷新列表
    this.loadDepartments()
  },
  // ------------------------------------------------------------------------------------------------------------------方法
  methods: {
    // ---------------------------------------------------------------查询企业的部门列表
    async loadDepartments() {
      // 发送Ajax
      const res = await getDepartments()
      // 删除数组第一个元素
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => {
        return {
          code: item.code,
          id: item.id,
          name: item.name,
          pid: item.pid
        }
      })
      // 绑定数据
      this.list = array2Tree(res.data.depts, '')
    },
    // ---------------------------------------------------------------添加部门功能
    hAdd(id) {
      this.isEdit = false
      // 绑定数据
      this.pid = id
      // 打开弹窗
      this.showDialog = true
    },
    // ---------------------------------------------------------------事件成功函数
    recSuccess() {
      // 关闭弹窗
      this.showDialog = false
      // 刷新列表
      this.loadDepartments()
    },
    // ---------------------------------------------------------------删除部门功能
    async hDel(id) {
      // 请求删除
      this.$confirm('此操作将删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartments(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      // 刷新列表
      this.loadDepartments()
    },
    // ---------------------------------------------------------------编辑部门功能
    hEdit(id) {
      this.isEdit = true
      this.pid = id
      this.showDialog = true
    },
    // ---------------------------------------------------------------子组件关闭弹窗功能
    recClose() {
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

